import http from './http'
import { cinemaUri } from "@/config/uri";
import { cityUri } from "@/config/uri"

// 获取影院列表信息
export const cinemaData = () =>{
  return http.get(cinemaUri,{
    headers: {
      'info' : 'cinema'
    }
  })
}

// 获取城市列表信息
export const cityData = async () =>{

  // 判断本地是否有缓存
  let cacheData = !localStorage.getItem('cityData') ? [] : JSON.parse(localStorage.getItem('cityData'))
  if(cacheData.length>0){
    return Promise.resolve(cacheData)
  }

  let ret = await http.get(cityUri,{
    headers: {
      'info' : 'city'
    }
  })
  // 定义城市字母索引
  let cityIndex = []
  let indexList = []

  let retData = ret.data.data.cities
  // 处理完后的数据
  let dataList = []
  for(let i = 65; i <=90; i++){
    cityIndex.push(String.fromCharCode(i))
  }
  console.log(cityIndex)
  cityIndex.forEach(index=>{
    let tmpArr = retData.filter(item=>{
      return index.toLowerCase() == item.pinyin.substr(0,1) 
    })

    if(tmpArr.length>0){
      indexList.push(index)
      dataList.push({
        index,
        data: tmpArr
      })
    }
  })
  let data = [dataList,indexList]
  localStorage.setItem('cityData', JSON.stringify(data))
  return Promise.resolve([dataList,indexList])
}